﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Threading;
using System.Diagnostics;
using System.IO;

using Factonomy.AzureUtility.Core;
using Factonomy.AzureUtility.Core.Storage;
using Factonomy.AzureUtility.Core.StorageHelpers;
using Factonomy.AzureUtility.Dialogs;

namespace Factonomy.AzureUtility.Controls
{
    class BlobContextMenu : BaseContextMenu
    {
        private BlobContainer _container = null;
        private MenuItem _editItem = new MenuItem();
        private MenuItem _downloadItem = new MenuItem();
        private MenuItem _copyItem = new MenuItem();
        private MenuItem _openItem = new MenuItem();
        private MenuItem _deleteItem = new MenuItem();

        public BlobContextMenu()
        {
            this._editItem.Header = "Edit";
            this._editItem.Click += new RoutedEventHandler(editItem_Click);
            this.Items.Add(this._editItem);
            
            this._downloadItem.Header = "Download";
            this._downloadItem.Click += new RoutedEventHandler(downloadItem_Click);
            this.Items.Add(this._downloadItem);
            
            this._copyItem.Header = "Copy URI to clipboard";
            this._copyItem.Click += new RoutedEventHandler(copyItem_Click);
            this.Items.Add(this._copyItem);
            
            this._openItem.Header = "Open URI in browser";
            this._openItem.Click += new RoutedEventHandler(openItem_Click);
            this.Items.Add(this._openItem);
            
            this._deleteItem.Header = "Delete";
            this._deleteItem.Click += new RoutedEventHandler(deleteItem_Click);
            this.Items.Add(this._deleteItem);

            this.Opened += new RoutedEventHandler(BlobContextMenu_Opened);
        }

        void BlobContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            try
            {
                ListView item = (ListView)((Popup)((ContextMenu)sender).Parent).PlacementTarget;
                BlobProperties properties = item.SelectedItem as BlobProperties;

                if (Globals.IsText(properties.ContentType, Path.GetExtension(properties.Name)))
                {
                    this._editItem.Visibility = Visibility.Visible;
                }
                else
                {
                    this._editItem.Visibility = Visibility.Collapsed;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to open URI.  " + ex.Message);
            }
        }

        public void LoadContainer(BlobContainer container)
        {
            this._container = container;
        }

        void openItem_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ListView item = (ListView)((Popup)((ContextMenu)(((MenuItem)sender).Parent)).Parent).PlacementTarget;
                BlobProperties properties = item.SelectedItem as BlobProperties;
                Process.Start(new ProcessStartInfo(properties.Uri.ToString()));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to open URI.  " + ex.Message);
            }
        }

        void copyItem_Click(object sender, RoutedEventArgs e)
        {
            ListView item = (ListView)((Popup)((ContextMenu)(((MenuItem)sender).Parent)).Parent).PlacementTarget;
            BlobProperties properties = item.SelectedItem as BlobProperties;
            Clipboard.SetDataObject(properties.Uri.ToString(), true);
        }

        void deleteItem_Click(object sender, RoutedEventArgs e)
        {
            ListView item = (ListView)((Popup)((ContextMenu)(((MenuItem)sender).Parent)).Parent).PlacementTarget;
            BlobProperties properties = item.SelectedItem as BlobProperties;
            if (MessageBox.Show("Really delete?", "Confirm delete", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                ConfigAccount account = Config.Instance.GetAccount(this._container.AccountName);
                BlobHelper.Instance.DeleteBlob(account, this._container.ContainerName, properties.Name);
            }
        }

        void downloadItem_Click(object sender, RoutedEventArgs e)
        {
            ListView item = (ListView)((Popup)((ContextMenu)(((MenuItem)sender).Parent)).Parent).PlacementTarget;
            BlobProperties properties = item.SelectedItem as BlobProperties;

            try
            {
                
                using (System.Windows.Forms.SaveFileDialog dialog = new System.Windows.Forms.SaveFileDialog())
                {
                    dialog.InitialDirectory = Environment.SpecialFolder.MyComputer.ToString();
                    dialog.FileName = Path.GetFileName(properties.Uri.ToString());
                    if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        
                        System.Net.WebClient client = new System.Net.WebClient();
                        client.DownloadFile(properties.Uri, dialog.FileName);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Download error.  " + ex.Message);
            }

        }

        void editItem_Click(object sender, RoutedEventArgs e)
        {
            ListView item = (ListView)((Popup)((ContextMenu)(((MenuItem)sender).Parent)).Parent).PlacementTarget;
            BlobProperties properties = item.SelectedItem as BlobProperties;

            //if (Globals.IsVideo(properties.ContentType, System.IO.Path.GetExtension(properties.Name)))
            //{
            //    MessageBox.Show("video");
            //}

            //if (Globals.IsImage(properties.ContentType, System.IO.Path.GetExtension(properties.Name)))
            //{
            //    ImageViewer imageViewer = new ImageViewer();
            //    imageViewer.Load(properties);
            //    imageViewer.ShowDialog(); 
            //}

            if (Globals.IsText(properties.ContentType, System.IO.Path.GetExtension(properties.Name)))
            {
                TextEditor editor = new TextEditor(properties, this._container);
                editor.Owner = Application.Current.Windows[0];
                editor.ShowDialog();
            }
        }
    }
}
